﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Framework;
using System.Diagnostics;
using System.IO;

namespace TeamDeploy.Tasks
{

    /// <summary>
    /// Sometimes the PsExec tool gets stuck running on the target machine.  This task attempts to cleanup psexec's mess.
    /// </summary>
    public class CleanupPsExec : TaskBase
    {
        public override bool Execute()
        {
            try
            {

                string args;
                Process proc;
                string message;

                //
                // The goal is to run this command:  sc \\[TargetMachine] stop psexesvc
                //
                Log.LogMessage("Stopping service psexesvc on " + TargetMachine + "...");
                args = @"\\" + TargetMachine + " stop psexesvc";
                proc = Process.Start("sc", args);
                proc.WaitForExit();

                message = "Stopping service psexesvc on " + TargetMachine + " returned code:" + proc.ExitCode;

                if (proc.ExitCode == 0)
                {
                    Log.LogMessage(message);
                }
                else
                {
                    Log.LogWarning(message);
                }


                // Incase the stop did not work, delete 
                bool killResult;
                KillProcess kill = new KillProcess();
                kill.BuildEngine = BuildEngine;
                kill.TargetMachine = TargetMachine;
                kill.KillAppPathFilename = KillAppPathFilename;
                kill.ProcessName = "psexesvc";
                kill.IgnoreExitCode = "true";
                killResult = kill.Execute();


                //
                // The goal is to run this command:  sc \\[TargetMachine] delete psexesvc
                //
                Log.LogMessage("Deleting service psexesvc on " + TargetMachine + "...");
                args = @"\\" + TargetMachine + " delete psexesvc";
                proc = Process.Start("sc", args);
                proc.WaitForExit();

                message = "Deleting service psexesvc on " + TargetMachine + " returned code:" + proc.ExitCode;

                if (proc.ExitCode == 0)
                {
                    Log.LogMessage(message);
                }
                else
                {
                    Log.LogWarning(message);
                }


                // delete the psexesvc file if it exists.
                File.Delete(@"\\" + TargetMachine + @"\admin$\psexesvc.exe");


                return true;
            }
            catch (Exception ex)
            {
                if (BuildEngine != null)
                {
                    Log.LogError("Error executing CleanupPsExec: TargetMachine=" +
                                 TargetMachine + " KillPathFilename=" + KillAppPathFilename + ":::" + ex);
                }
                return false;
            }
        }

        [Required]
        public string TargetMachine { get; set; }

        [Required]
        public string KillAppPathFilename { get; set; }
    }
}
